package com.hera.base.web.vaadin.mantenimiento;

import static com.hera.base.util.InputData.getStringData;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.FiltroDTO;
import com.hera.base.util.TableCustom;
import com.hera.base.web.BaseListadoListener;
import com.hera.base.web.BaseUI;
import com.hera.base.web.MantenimientoMaestroDetalle;
import com.hera.process.config.rest.delegate.WorkflowDelegate;
import com.hera.process.security.domain.User;
import com.hera.process.workflow.domain.ProcessGroup;
import com.hera.process.workflow.domain.ProcessGroupUser;
import com.hera.process.workflow.domain.ProcessGroupUserPK;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class ProcessGroupUI extends BaseUI implements
		MantenimientoMaestroDetalle, ClickListener, BaseListadoListener {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_2;
	@AutoGenerated
	private HorizontalLayout lyt_DetallePG;
	@AutoGenerated
	private VerticalLayout pnlDetalle;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_5;
	@AutoGenerated
	private Button btnNuevoPGS;
	@AutoGenerated
	private Button btnEliminarPGS;
	@AutoGenerated
	private TextField txtNombres_PGU;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_2;
	@AutoGenerated
	private Button btnBuscar_PG;
	@AutoGenerated
	private TextField txtUser_PGU;
	@AutoGenerated
	private Label lbltitulo;
	@AutoGenerated
	private HorizontalLayout lytTablaDetalle;
	@AutoGenerated
	private TableCustom tblfDetalleProcessG;
	@AutoGenerated
	private HorizontalLayout lyt_MaestroPG;
	@AutoGenerated
	private VerticalLayout pnlFormulario;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_3;
	@AutoGenerated
	private Button btnEliminarPG;
	@AutoGenerated
	private Button btnNuevoPG;
	@AutoGenerated
	private TextArea txtDes_PG;
	@AutoGenerated
	private TextField txtNombre_PG;
	@AutoGenerated
	private Label label_2;
	@AutoGenerated
	private HorizontalLayout lytTablaMaestra;
	@AutoGenerated
	private TableCustom tblfProcessG;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The constructor should first build the main layout, set the composition
	 * root and then do any custom initialization.
	 * 
	 * The constructor will not be automatically regenerated by the visual
	 * editor.
	 */
	public ProcessGroupUI() {
		buildMainLayout();
		setCompositionRoot(mainLayout);

		// TODO add user code here
	}

	
	@Autowired
	WorkflowDelegate workflowDelegate;
	boolean esNuevo;
	boolean esNuevoDetalle;
	User user;
	ProcessGroup processGroup;
	ProcessGroupUser proccesGroupuser;
	ProcessGroupUserPK pgPK;

	// ParameterDetail parameterDetail;

	@Override
	protected void postConstruct() {
		cargarFormulario();
		buscarMaestro();
		buscarDetalle();
		HabilitarControlesDetalle(false);

	}

	@Override
	public void buttonClick(ClickEvent event) {
		if (event.getButton().equals(btnEliminarPG)) {
			eliminarMaestro();
		} else if (event.getButton().equals(btnNuevoPG)) {
			guardarMaestro();
		}  else if (event.getButton().equals(btnBuscar_PG)) {
			final ListadoPGUSerUI content = new ListadoPGUSerUI(this);
			mostrarModal(content, "Buscar Usuarios", "350px", "600px");
		}else if(event.getButton().equals(btnEliminarPGS)){
			eliminarDetalle();
		}else if(event.getButton().equals(btnNuevoPGS)){
			guardarDetalle();
		}		
	}

	private static final Logger log = Logger.getLogger(ProcessGroupUI.class);

	public void HabilitarControlesDetalle(boolean habilitar){
		btnEliminarPGS.setEnabled(habilitar);
		btnNuevoPGS.setEnabled(habilitar);
		btnBuscar_PG.setEnabled(habilitar);
	}
	
	@Override
	public void cargarFormulario() {
		log.info("inicio cargar formulario");
		esNuevo = true;
		processGroup = null;
		proccesGroupuser = null;

		txtNombre_PG.addStyleName("fieldRequired");
		txtDes_PG.addStyleName("fieldRequired");

		btnEliminarPG.setVisible(false);
		btnEliminarPG.addClickListener(this);
		btnNuevoPG.addClickListener(this);
		btnNuevoPG.setCaption("Nuevo");
		
		btnEliminarPGS.setVisible(false);
		btnEliminarPGS.addClickListener(this);
		btnNuevoPGS.addClickListener(this);
		btnNuevoPGS.setCaption("Nuevo");
		
		btnBuscar_PG.addClickListener(this);
		txtUser_PGU.setEnabled(false);
		txtNombres_PGU.setEnabled(false);
		

		tblfProcessG.setTitulo("Lista de Grupos Procesos");
		tblfProcessG.setHeight("200px");

		tblfProcessG.addColumn("name", "Nombre", 200, true, String.class);
		tblfProcessG.addColumn("description", "Descripcion", 200, true,	String.class);

		tblfProcessG.construir();

		tblfProcessG.addTextChangeListener(new TextChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void textChange(TextChangeEvent event) {
				buscarMaestro(event);

			}
		});
		tblfProcessG.addValueChangeListener(new ValueChangeListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				processGroup = (ProcessGroup) tblfProcessG.getSelectedItem(event);
				esNuevo = (processGroup == null);
				tblfDetalleProcessG.limpiarFiltro();
				if (esNuevo) {
					limpiarMaestro();
					limpiarDetalle();
					btnEliminarPG.setVisible(false);
					HabilitarControlesDetalle(false);
					btnNuevoPGS.setVisible(true);
					tblfDetalleProcessG.setLstData(new ArrayList<ProcessGroup>());
				} else {
					btnEliminarPG.setVisible(true);
					btnNuevoPG.setVisible(true);
					btnNuevoPG.setCaption("Modificar");					
					HabilitarControlesDetalle(true);
					txtNombre_PG.setValue(processGroup.getName());
					txtDes_PG.setValue(processGroup.getDescription());
					buscarDetalle();
					limpiarDetalle();

				}

			}
		});

		tblfDetalleProcessG.setHeight("200px");
		tblfDetalleProcessG.setTitulo("Lista de Usuario");
		tblfDetalleProcessG.addColumn("id.userLogin", "Login", 200, true,String.class);
		tblfDetalleProcessG.addColumn("userNames", "Nombre", 200, true,	String.class);

		tblfDetalleProcessG.construir();
		tblfDetalleProcessG.addTextChangeListener(new TextChangeListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void textChange(TextChangeEvent event) {
				buscarDetalle(event);
			}

		});
		tblfDetalleProcessG.addValueChangeListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				proccesGroupuser = (ProcessGroupUser) tblfDetalleProcessG
						.getSelectedItem(event);
				esNuevoDetalle = (proccesGroupuser == null);
				if (esNuevoDetalle) {
					HabilitarControlesDetalle(false);
					limpiarDetalle();
				} else {
					HabilitarControlesDetalle(true);
					btnEliminarPGS.setVisible(true);
					
					txtUser_PGU.setValue(proccesGroupuser.getId().getUserLogin());

					txtNombres_PGU.setValue(proccesGroupuser.getUserNames());
				}
			}
		});

		log.info("fin cargarFormulario");

	}

	@Override
	public void buscarMaestro() {
		buscarMaestro(null);

	}

	@Override
	public void buscarMaestro(TextChangeEvent event) {
		log.info("inicio Buscar DataSource");
	
		
		FiltroDTO filtroDTO=tblfProcessG.getFiltroDTO(event);

		List<ProcessGroup> lstprocessGroup = workflowDelegate
				.findAllProcessGroups(filtroDTO);
		tblfProcessG.setLstData(lstprocessGroup);
		limpiarMaestro();
		log.info("fin ProcessGroup");

	}

	@Override
	public void limpiarMaestro() {
		log.info("inicio limpiarPROCESSGROUP");
		btnNuevoPG.setVisible(true);
		btnNuevoPG.setCaption("Nuevo");
		txtNombre_PG.setValue("");
		txtDes_PG.setValue("");
		//

		log.info("fin limpiarPROCESSGROUP");

	}

	@Override
	public void eliminarMaestro() {
		log.info("inicio eliminarprocessGroup");
		workflowDelegate.deleteProcessgroup(processGroup);
		buscarMaestro();
		limpiarMaestro();
		log.info("fin eliminarprocessGroup");

	}

	@Override
	public void guardarMaestro() {
		log.info("inicio guardarPROCESSGROUP");
		if (esNuevo) {
			processGroup = new ProcessGroup();
			obtenerDatosFormulario(processGroup);
			workflowDelegate.createProcessGroup(processGroup);
		} else {
			obtenerDatosFormulario(processGroup);
			workflowDelegate.updateProcessGroup(processGroup);
		}
		limpiarMaestro();
		buscarMaestro();
		log.info("fin guardarPROCESSGROUP");

	}

	@Override
	public void buscarDetalle() {
		buscarDetalle(null);

	}

	@Override
	public void buscarDetalle(TextChangeEvent event) {
		if (processGroup == null) {

			tblfDetalleProcessG.setLstData(new ArrayList<ProcessGroupUser>());
			return;
		}
		FiltroDTO filtroDTO = tblfDetalleProcessG.getFiltroDTO(event);
		filtroDTO.addParametro("id.idProcessGroup",
				processGroup.getIdProcessGroup());
		// filtroDTO.addOrdenamientoAscendente("order");
		// filtroDTO.addOrdenamientoAscendente("name");
		List<ProcessGroupUser> lstPGDetail = workflowDelegate
				.findAllProcessGroupUsers(filtroDTO);
		tblfDetalleProcessG.setLstData(lstPGDetail);
		limpiarDetalle();

	}

	@Override
	public void limpiarDetalle() {
		

		
		
		log.info("inicio limpiarDetalle");		
		btnEliminarPGS.setVisible(false);
		btnNuevoPGS.setCaption("Nuevo");
		btnNuevoPGS.setVisible(true);
		txtUser_PGU.setValue("");
		txtNombres_PGU.setValue("");
		log.info("fin limpiarDetalle");

	}

	@Override
	public void eliminarDetalle() {
		workflowDelegate.deleteProcessGroupUser(proccesGroupuser);

		buscarDetalle();
		limpiarDetalle();

	}

	@Override
	public void guardarDetalle() {
		proccesGroupuser = new ProcessGroupUser();
		if (esNuevoDetalle) {

			
			obtenerDatosFormulario(proccesGroupuser);
			proccesGroupuser.setProcessGroup(processGroup);
			workflowDelegate.createProcessGroupUser(proccesGroupuser);

		} else {
			obtenerDatosFormulario(proccesGroupuser);
			workflowDelegate.updateProcessGroupUser(proccesGroupuser);
		}
		limpiarDetalle();
		buscarDetalle();
	
	}

	private void obtenerDatosFormulario(ProcessGroup processGroup) {

		processGroup.setName(getStringData(txtNombre_PG, "Name"));
		processGroup.setDescription(getStringData(txtDes_PG, "description"));

	}

	private void obtenerDatosFormulario(ProcessGroupUser processGroupUser) {

		processGroupUser.setId(new ProcessGroupUserPK());
		processGroupUser.getId().setUserLogin(getStringData(txtUser_PGU, "Login"));
		processGroupUser.getId().setIdProcessGroup(processGroup.getIdProcessGroup());
		processGroupUser.setUserNames(getStringData(txtNombres_PGU, "Nombres"));

	}

	@Override
	public void recibirDatos(Object datos) {
		User user=(User)datos;
		
		txtUser_PGU.setValue(user.getUserLogin());
		txtNombres_PGU.setValue(user.getFullName());
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("-1px");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("-1px");
		setHeight("100.0%");
		
		// verticalLayout_2
		verticalLayout_2 = buildVerticalLayout_2();
		mainLayout.addComponent(verticalLayout_2);
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_2() {
		// common part: create layout
		verticalLayout_2 = new VerticalLayout();
		verticalLayout_2.setImmediate(false);
		verticalLayout_2.setWidth("-1px");
		verticalLayout_2.setHeight("-1px");
		verticalLayout_2.setMargin(false);
		
		// lyt_MaestroPG
		lyt_MaestroPG = buildLyt_MaestroPG();
		verticalLayout_2.addComponent(lyt_MaestroPG);
		
		// lyt_DetallePG
		lyt_DetallePG = buildLyt_DetallePG();
		verticalLayout_2.addComponent(lyt_DetallePG);
		
		return verticalLayout_2;
	}

	@AutoGenerated
	private HorizontalLayout buildLyt_MaestroPG() {
		// common part: create layout
		lyt_MaestroPG = new HorizontalLayout();
		lyt_MaestroPG.setImmediate(false);
		lyt_MaestroPG.setWidth("-1px");
		lyt_MaestroPG.setHeight("-1px");
		lyt_MaestroPG.setMargin(false);
		lyt_MaestroPG.setSpacing(true);
		
		// lytTablaMaestra
		lytTablaMaestra = buildLytTablaMaestra();
		lyt_MaestroPG.addComponent(lytTablaMaestra);
		
		// pnlFormulario
		pnlFormulario = buildPnlFormulario();
		lyt_MaestroPG.addComponent(pnlFormulario);
		
		return lyt_MaestroPG;
	}

	@AutoGenerated
	private HorizontalLayout buildLytTablaMaestra() {
		// common part: create layout
		lytTablaMaestra = new HorizontalLayout();
		lytTablaMaestra.setImmediate(false);
		lytTablaMaestra.setWidth("480px");
		lytTablaMaestra.setHeight("-1px");
		lytTablaMaestra.setMargin(false);
		lytTablaMaestra.setSpacing(true);
		
		// tblfProcessG
		tblfProcessG = new TableCustom();
		tblfProcessG.setImmediate(false);
		tblfProcessG.setWidth("-1px");
		tblfProcessG.setHeight("-1px");
		lytTablaMaestra.addComponent(tblfProcessG);
		
		return lytTablaMaestra;
	}

	@AutoGenerated
	private VerticalLayout buildPnlFormulario() {
		// common part: create layout
		pnlFormulario = new VerticalLayout();
		pnlFormulario.setImmediate(false);
		pnlFormulario.setWidth("-1px");
		pnlFormulario.setHeight("-1px");
		pnlFormulario.setMargin(false);
		pnlFormulario.setSpacing(true);
		
		// label_2
		label_2 = new Label();
		label_2.setStyleName("titulo");
		label_2.setImmediate(false);
		label_2.setWidth("-1px");
		label_2.setHeight("-1px");
		label_2.setValue("Grupo Procesos");
		pnlFormulario.addComponent(label_2);
		pnlFormulario.setComponentAlignment(label_2, new Alignment(20));
		
		// txtNombre_PG
		txtNombre_PG = new TextField();
		txtNombre_PG.setImmediate(false);
		txtNombre_PG.setDescription("Nombre");
		txtNombre_PG.setWidth("200px");
		txtNombre_PG.setHeight("-1px");
		txtNombre_PG.setInputPrompt("Nombre");
		pnlFormulario.addComponent(txtNombre_PG);
		
		// txtDes_PG
		txtDes_PG = new TextArea();
		txtDes_PG.setImmediate(false);
		txtDes_PG.setDescription("descripcion");
		txtDes_PG.setWidth("200px");
		txtDes_PG.setHeight("-1px");
		txtDes_PG.setInputPrompt("descripcion");
		pnlFormulario.addComponent(txtDes_PG);
		
		// horizontalLayout_3
		horizontalLayout_3 = buildHorizontalLayout_3();
		pnlFormulario.addComponent(horizontalLayout_3);
		
		return pnlFormulario;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_3() {
		// common part: create layout
		horizontalLayout_3 = new HorizontalLayout();
		horizontalLayout_3.setImmediate(false);
		horizontalLayout_3.setWidth("-1px");
		horizontalLayout_3.setHeight("-1px");
		horizontalLayout_3.setMargin(false);
		horizontalLayout_3.setSpacing(true);
		
		// btnNuevoPG
		btnNuevoPG = new Button();
		btnNuevoPG.setCaption("Nuevo");
		btnNuevoPG.setImmediate(true);
		btnNuevoPG.setWidth("-1px");
		btnNuevoPG.setHeight("-1px");
		horizontalLayout_3.addComponent(btnNuevoPG);
		
		// btnEliminarPG
		btnEliminarPG = new Button();
		btnEliminarPG.setCaption("Eliminar");
		btnEliminarPG.setImmediate(true);
		btnEliminarPG.setWidth("-1px");
		btnEliminarPG.setHeight("-1px");
		horizontalLayout_3.addComponent(btnEliminarPG);
		
		return horizontalLayout_3;
	}

	@AutoGenerated
	private HorizontalLayout buildLyt_DetallePG() {
		// common part: create layout
		lyt_DetallePG = new HorizontalLayout();
		lyt_DetallePG.setImmediate(false);
		lyt_DetallePG.setWidth("-1px");
		lyt_DetallePG.setHeight("-1px");
		lyt_DetallePG.setMargin(false);
		lyt_DetallePG.setSpacing(true);
		
		// lytTablaDetalle
		lytTablaDetalle = buildLytTablaDetalle();
		lyt_DetallePG.addComponent(lytTablaDetalle);
		
		// pnlDetalle
		pnlDetalle = buildPnlDetalle();
		lyt_DetallePG.addComponent(pnlDetalle);
		
		return lyt_DetallePG;
	}

	@AutoGenerated
	private HorizontalLayout buildLytTablaDetalle() {
		// common part: create layout
		lytTablaDetalle = new HorizontalLayout();
		lytTablaDetalle.setImmediate(false);
		lytTablaDetalle.setWidth("480px");
		lytTablaDetalle.setHeight("-1px");
		lytTablaDetalle.setMargin(false);
		
		// tblfDetalleProcessG
		tblfDetalleProcessG = new TableCustom();
		tblfDetalleProcessG.setImmediate(false);
		tblfDetalleProcessG.setWidth("-1px");
		tblfDetalleProcessG.setHeight("-1px");
		lytTablaDetalle.addComponent(tblfDetalleProcessG);
		
		return lytTablaDetalle;
	}

	@AutoGenerated
	private VerticalLayout buildPnlDetalle() {
		// common part: create layout
		pnlDetalle = new VerticalLayout();
		pnlDetalle.setImmediate(false);
		pnlDetalle.setWidth("-1px");
		pnlDetalle.setHeight("-1px");
		pnlDetalle.setMargin(false);
		pnlDetalle.setSpacing(true);
		
		// lbltitulo
		lbltitulo = new Label();
		lbltitulo.setStyleName("titulo");
		lbltitulo.setImmediate(false);
		lbltitulo.setWidth("-1px");
		lbltitulo.setHeight("-1px");
		lbltitulo.setValue("Usuario");
		pnlDetalle.addComponent(lbltitulo);
		pnlDetalle.setComponentAlignment(lbltitulo, new Alignment(20));
		
		// horizontalLayout_2
		horizontalLayout_2 = buildHorizontalLayout_2();
		pnlDetalle.addComponent(horizontalLayout_2);
		
		// txtNombres_PGU
		txtNombres_PGU = new TextField();
		txtNombres_PGU.setStyleName("fieldRequired");
		txtNombres_PGU.setImmediate(false);
		txtNombres_PGU.setDescription("Nombres");
		txtNombres_PGU.setWidth("200px");
		txtNombres_PGU.setHeight("-1px");
		txtNombres_PGU.setInputPrompt("Nombres");
		pnlDetalle.addComponent(txtNombres_PGU);
		
		// horizontalLayout_5
		horizontalLayout_5 = buildHorizontalLayout_5();
		pnlDetalle.addComponent(horizontalLayout_5);
		
		return pnlDetalle;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_2() {
		// common part: create layout
		horizontalLayout_2 = new HorizontalLayout();
		horizontalLayout_2.setImmediate(false);
		horizontalLayout_2.setWidth("-1px");
		horizontalLayout_2.setHeight("-1px");
		horizontalLayout_2.setMargin(false);
		horizontalLayout_2.setSpacing(true);
		
		// txtUser_PGU
		txtUser_PGU = new TextField();
		txtUser_PGU.setImmediate(false);
		txtUser_PGU.setDescription("Login");
		txtUser_PGU.setWidth("-1px");
		txtUser_PGU.setHeight("-1px");
		txtUser_PGU.setInputPrompt("Login");
		horizontalLayout_2.addComponent(txtUser_PGU);
		
		// btnBuscar_PG
		btnBuscar_PG = new Button();
		btnBuscar_PG.setCaption(".....");
		btnBuscar_PG.setImmediate(true);
		btnBuscar_PG.setWidth("-1px");
		btnBuscar_PG.setHeight("-1px");
		horizontalLayout_2.addComponent(btnBuscar_PG);
		
		return horizontalLayout_2;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_5() {
		// common part: create layout
		horizontalLayout_5 = new HorizontalLayout();
		horizontalLayout_5.setImmediate(false);
		horizontalLayout_5.setWidth("-1px");
		horizontalLayout_5.setHeight("-1px");
		horizontalLayout_5.setMargin(false);
		horizontalLayout_5.setSpacing(true);
		
		// btnEliminarPGS
		btnEliminarPGS = new Button();
		btnEliminarPGS.setCaption("Eliminar");
		btnEliminarPGS.setImmediate(true);
		btnEliminarPGS.setWidth("-1px");
		btnEliminarPGS.setHeight("-1px");
		horizontalLayout_5.addComponent(btnEliminarPGS);
		
		// btnNuevoPGS
		btnNuevoPGS = new Button();
		btnNuevoPGS.setCaption("Nuevo");
		btnNuevoPGS.setImmediate(true);
		btnNuevoPGS.setWidth("-1px");
		btnNuevoPGS.setHeight("-1px");
		horizontalLayout_5.addComponent(btnNuevoPGS);
		
		return horizontalLayout_5;
	}

}
